package com.voxel.sense.infomsg.module.task;

import com.voxel.sense.infomsg.base.RecyclerViewPresenter;
import com.voxel.sense.infomsg.data.bean.ConditionBean;
import com.voxel.sense.infomsg.module.task.manager.TaskManagerContract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.model.api.response.QueryConditionEntity;

/**
 * Created by ChenYiYao on 2018/3/23.
 */

public abstract class BaseTaskPresenter<View extends TaskManagerContract.View> extends RecyclerViewPresenter<View> implements TaskManagerContract.Presenter {

    public BaseTaskPresenter(View view) {
        super(view);
    }

    protected int CURRENT_PAGE_SIZE = 1;//加载数据的页数
    protected Map<String, Object> mPrepareQueryMap;//记录没有提交前选中的条件
    protected Map<String, Object> mQueryMap;//点击提交后的条件
    protected String mPrepareSortType;//记录没有提交前选中的排序条件
    protected String mSortType;
    protected QueryConditionEntity mSearchconfig;//搜索条件数据

    @Override
    public void start() {
        super.start();
        mPrepareQueryMap = new HashMap<>();
        mPrepareQueryMap.put(Constants.TYPE_AREA, new ArrayList());
        mPrepareQueryMap.put(Constants.TYPE_IDUSTRY, new ArrayList());
        mQueryMap = new HashMap<>();
//        mSearchconfig = QueryConfigManager.getInstance().getSearchMenuconfig(false);
    }

    @Override
    public void doLoadRegionMenuData(String city) {
        if (mSearchconfig == null)
            return;
        List<ConditionBean> list = new ArrayList<>();
        List<QueryConditionEntity.BodyBean.AreaDataBean> cityList = mSearchconfig.getBody().getAreaData();
        if (cityList != null && cityList.size() > 0) {
            for (QueryConditionEntity.BodyBean.AreaDataBean bean : cityList) {
                if (bean.getCityName() != null && bean.getCityName().equals(city)) {
                    for (QueryConditionEntity.BodyBean.AreaDataBean.AreasBean area : bean.getAreas()) {
                        ConditionBean conditionBean = new ConditionBean(Constants.TYPE_AREA, area.getAreaId(), area.getAreaName(), false);
                        list.add(conditionBean);
                    }
                }
            }
            mView.onNotifyRegionData(list);
        }
    }

    @Override
    public void doLoadTaskTypeMenuData() {
        if (mSearchconfig == null)
            return;
        List<ConditionBean> list = new ArrayList<>();
//        List<ConfigEntity.BodyBean.TaskTypesBean> taskType = QueryConfigManager.getInstance().getTaskType();

//        if (taskType != null) {
//            for (ConfigEntity.BodyBean.TaskTypesBean bean : taskType) {
//                ConditionBean conditionBean = new ConditionBean(Constants.TYPE_IDUSTRY, bean.getTaskTypeId(), bean.getTaskTypeName(), false);
//                list.add(conditionBean);
//            }
//        } else {
//            ToastUtil.showToastLong(Factory.app(), "加载任务类型数据失败");
//        }
        mView.onNotifyTaskTypeData(list);
    }

    @Override
    public void doLoadOtherMenuData() {

        if (mSearchconfig == null)
            return;
        List<ConditionBean> list = new ArrayList<>();
        ConditionBean conditionBean = new ConditionBean(Constants.Type_Smart, "1",
                "时间顺序排序", false);
        list.add(conditionBean);
        ConditionBean conditionBean1 = new ConditionBean(Constants.Type_Smart, "0",
                "时间倒叙排序", false);
        list.add(conditionBean1);
        mView.onNotifyOtherData(list);

    }

    @Override
    public void doLoadData() {
        startRefreshData();
    }

    @Override
    public void doLoadMoreData() {
        startLoadMoreData();
    }


    @Override
    public void doSubmitSearchCondition() {
        mQueryMap.clear();
        mQueryMap.putAll(mPrepareQueryMap);
        mSortType = mPrepareSortType;
    }

    @Override
    public void changeSearchCondition(ConditionBean bean) {
        switch (bean.getType()) {
            case Constants.TYPE_AREA://地区
                List listArea = (List) mPrepareQueryMap.get(Constants.TYPE_AREA);
                if (bean.isSelect()) {
                    listArea.add(bean.getDesc());
                } else {
                    listArea.remove(bean.getDesc());
                }
                break;
            case Constants.TYPE_IDUSTRY://任务类型
                List listIdustry = (List) mPrepareQueryMap.get(Constants.TYPE_IDUSTRY);
                if (bean.isSelect()) {
                    listIdustry.add(bean.getTipCode());
                } else {
                    listIdustry.remove(bean.getTipCode());
                }
                break;
            case Constants.Type_Smart://智能筛选
                //  todo:目前该类型只是单选，后期应该会改动
                if (bean.isSelect()) {
                    mPrepareSortType = bean.getTipCode();
                } else {
                    mPrepareSortType = "";
                }
                break;
        }
    }


}
